package com.vend.common.retrofithttp;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.MalformedJsonException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by glendal on 2022/1/13.
 * Http响应数据回调方法。
 */

public abstract class HttpCallback<T> implements Callback<T> {

    private static final String TAG = "HttpCallback";


    @Override
    public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
        if (response.code() == 200) {//http响应成功
            T result = response.body();
            if (result != null) {
                doOnSuccess(result, response);
            } else {
                try {
                    if (response.errorBody() == null) {
                        doOnFailure(-1, "服务器繁忙,错误码:" + response.code());
                    } else {
                        doOnFailure(-1, response.errorBody().string());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    doOnFailure(-1, e.getMessage());
                }
            }
        } else if ((response.code() + "").startsWith("5")) {
            doOnFailure(response.code(), "服务器繁忙,错误码:" + response.code());
        } else {
            //http响应失败
            Log.i(TAG, "onFailure");
            try {
                if (response.errorBody() == null) {
                    doOnFailure(response.code(), "服务器繁忙,错误码:" + response.code());
                } else {
                    doOnFailure(response.code(), response.errorBody().string());
                }
            } catch (IOException e) {
                doOnFailure(response.code(), "异常出错:错误信息:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        Log.i(TAG, "onFailure");
        t.printStackTrace();
        if (!call.isCanceled()) {
            String failureMsg = t.getMessage();
            if (t instanceof InterruptedIOException) {
                failureMsg = "连接超时！";
            } else if (t instanceof UnknownServiceException) {
                failureMsg = "服务器繁忙!";
            } else if (t instanceof UnknownHostException) {
                //            failureMsg = "服务器异常！";
                failureMsg = "服务器连接失败，请检查网络！";
            } else if (t instanceof ConnectException) {
                failureMsg = "服务器连接失败，请检查网络！";
            } else if (t instanceof SocketException) {
                failureMsg = "网络异常！";
            } else if (t instanceof JsonSyntaxException ) {
                failureMsg = "数据转换出错！";
            } else if(t instanceof MalformedJsonException) {
                failureMsg = "JSON格式错误";
            }
            doOnFailure(0, failureMsg);
        }
    }

    public abstract void doOnSuccess(@NonNull T result, Response<T> response);

    public abstract void doOnFailure(int httpCode, String message);
}
